using System;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

using RealDolmen.SharePoint.Helpers;

namespace RealDolmen.SharePoint.ListFeatures
{
    public class ListFeatureEmailEventReceiver : SPEmailEventReceiver
    {
        public override void EmailReceived(SPList list, SPEmailMessage emailMessage, string receiverData)
        {
            SPWeb web;
            SPSite site;
            ListFeature listFeature = ListFeatureTracker.Instantiate(new ListFeatureCompiledReceiverData(receiverData).TrackerData).OpenListFeature(out web, out site);

            if (listFeature != null)
            {
                if (listFeature.IsActive)
                {
                    listFeature.Register(this);
                    EmailReceived(list, listFeature, emailMessage, receiverData);
                    SPHelper.TryDisposeWeb(web);
                    SPHelper.TryDisposeSite(site);
                }
            }

        }

        public virtual void EmailReceived(SPList list, ListFeature listFeature, SPEmailMessage emailMessage, string receiverData)
        {
            throw new InvalidOperationException();
        }
    }
}
